package com.file.system.controller.system;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.dict.OnOff;
import com.file.system.entity.res.CommonResult;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebFilterWord;
import com.file.system.service.system.IWebFilterWordService;
import com.file.system.tools.regular.RegularUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

import static cn.hutool.core.text.CharSequenceUtil.isNotEmpty;


/**
 * 过滤词汇
 *
 * @author jch
 */
@RestController
@Validated
@RequestMapping("/web/filter/word")
public class WebFilterWordController {

    // 注入拦截字段服务
    @Autowired
    private IWebFilterWordService service;

    /**
     * 查询过滤词汇分页
     *
     * @param pageIndex 页数
     * @param pageSize  每页条数
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public CommonResult<DataPage<List<WebFilterWord>>> select(@PathVariable
                                                              @Min(value = 1, message = "页数最小1")
                                                              Integer pageIndex,
                                                              @PathVariable
                                                              @Min(value = 1, message = "每页条数最小1")
                                                              @Max(value = 100, message = "每页条数最大100")
                                                              Integer pageSize,
                                                              @RequestBody @Validated WebFilterWord webFilterWord
    ) {
        QueryWrapper<WebFilterWord> query = new QueryWrapper<>();
        //排序
        query.orderByDesc("a.id");
        //查询条件
        query.eq("a.is_del", 0);
        //查询条件
        query.like(isNotEmpty(webFilterWord.getFilterName()), "a.name", webFilterWord.getFilterName());
        query.like(isNotEmpty(webFilterWord.getDictOnOff()), "a.dict_on_off", webFilterWord.getDictOnOff());
        //查询集合
        DataPage<List<WebFilterWord>> dp = service.page(pageIndex, pageSize, query);
        return CommonResult.ok().setResult(dp);
    }


    /**
     * 通过ids查询过滤词汇集合
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listByIds")
    public CommonResult<List<WebFilterWord>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<WebFilterWord> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
     * 查询所有非删除状态过滤词汇
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/listAll")
    public CommonResult<List<WebFilterWord>> listAll() {
        //查询集合
        List<WebFilterWord> list = service.listAll();
        return CommonResult.ok().setResult(list);
    }


    /**
     * 通过id查询过滤词汇
     *
     * @param id 过滤词汇id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/id/{id:" + RegularUtil.ID + "}")
    public CommonResult<WebFilterWord> getById(@PathVariable
                                               @Min(value = 1, message = "id长度最小1")
                                               Long id) {
        //查询集合
        WebFilterWord webFilterWord = service.getById(id);
        return CommonResult.ok().setResult(webFilterWord);
    }


    /**
     * 通过id删除过滤词汇
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/remove/{id:" + RegularUtil.ID + "}")
    public CommonResult removeById(@PathVariable
                                   @Min(value = 1, message = "id长度最小1")
                                   Long id) {
        boolean state = service.removeById(id);
        //返回
        return CommonResult.check(state);
    }


    /**
     * 通过ids删除过滤词汇
     *
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/removeByIds")
    public CommonResult removeById(@RequestBody List<Long> ids) {
        //返回
        return CommonResult.check(service.removeByIds(ids));
    }


    /**
     * 通过id编辑过滤词汇
     *
     * @param webFilterWord 过滤词汇
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/updateById")
    public CommonResult<WebFilterWord> updateById(@RequestBody @Validated(WebFilterWord.update.class) WebFilterWord webFilterWord) {
        //编辑修改
        boolean state = service.updateById(webFilterWord);
        return CommonResult.check(state).setResult(webFilterWord);
    }


    /**
     * 添加过滤词汇
     *
     * @param webFilterWord 过滤词汇
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/save")
    public CommonResult<WebFilterWord> save(@RequestBody @Validated(WebFilterWord.add.class) WebFilterWord webFilterWord) {
        //添加过滤词汇
        boolean state = service.save(webFilterWord);
        return CommonResult.check(state).setResult(webFilterWord);
    }


    /**
     * 启用禁用
     *
     * @param id id
     * @return CommonResult
     * @apiNote -
     * @author jch
     */
    @PostMapping("/isStart/{id}")
    public CommonResult start(@PathVariable
                              @Min(value = 1, message = "id长度最小1")
                              Long id) {
        WebFilterWord webFilterWord = service.getById(id);
        //判断当前状态是否为开启状态
        if (ObjectUtil.equals(OnOff.ON.val, webFilterWord.getDictOnOff())) {
            //如果当前状态为开启状态，则将状态设置为关闭
            webFilterWord.setDictOnOff(OnOff.OFF.val);
        } else {
            //如果当前状态为关闭状态，则将状态设置为开启
            webFilterWord.setDictOnOff(OnOff.ON.val);
        }
        //更新过滤缓存
        boolean state = service.updateById(webFilterWord);
        return CommonResult.check(state);
    }
}

